package com.cn.suplayer.seahcontrol;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.VideoColumns;
import android.util.Log;

import com.cn.suplayer.bean.VideoDirectories;
import com.cn.suplayer.bean.VideoModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ZLM on 2016/6/16.
 * 搜索控制
 */
public class SearchControl {


    private ContentResolver contentResolver;

    public SearchControl(Context context) {
        contentResolver = context.getContentResolver();
    }


    public List<VideoModel> getCurrentDirector() {

        List<VideoModel> videoModels = new ArrayList<>();

        Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                null, null, null, VideoColumns.DATE_ADDED);

        if (cursor == null || !cursor.moveToNext())
            return videoModels;

        cursor.moveToLast();

        do {
            VideoModel model = new VideoModel();
            model.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.TITLE)));
            model.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.ALBUM)));
            model.setDisplayName(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DISPLAY_NAME)));
            model.setPath(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DATA)));
            model.setDateAdd(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DATE_ADDED)));
            model.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(VideoColumns.DURATION)));
            model.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(VideoColumns.SIZE)));

            videoModels.add(model);
        } while (cursor.moveToPrevious());

        cursor.close();
        return videoModels;
    }

    public List<VideoDirectories> getDirectories() {
        List<VideoDirectories> videoDirectories = new ArrayList<>();
        Map<String, VideoDirectories> map = new HashMap<>();
        try {
            Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    null, null, null, VideoColumns.DATE_ADDED);

            if (cursor == null || !cursor.moveToNext())
                return videoDirectories;


            cursor.moveToLast();

            do {

                String name = cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.BUCKET_DISPLAY_NAME));
                VideoModel model = new VideoModel();
                model.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.TITLE)));
                model.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.ALBUM)));
                model.setDisplayName(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DISPLAY_NAME)));
                model.setPath(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DATA)));
                model.setDateAdd(cursor.getString(cursor.getColumnIndexOrThrow(VideoColumns.DATE_ADDED)));
                model.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(VideoColumns.DURATION)));
                model.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(VideoColumns.SIZE)));

                if (map.keySet().contains(name)) {
                    map.get(name).increaseCount();
                    map.get(name).getVideos().add(model);
                } else {
                    VideoDirectories directories = new VideoDirectories();
                    directories.setName(name);
                    directories.setVideos(new ArrayList<VideoModel>());
                    directories.getVideos().add(model);
                    directories.increaseCount();

                    map.put(name, directories);

                    videoDirectories.add(directories);
                }


            } while (cursor.moveToPrevious());

            cursor.close();
        } catch (Exception e) {
            Log.e("error", e + "");
            e.printStackTrace();
        }

        return videoDirectories;
    }
}
